package wiki.xsx.jg.core;

import java.sql.Connection;
import java.util.*;

/**
 * 数据库对象
 */
public class Database {
    //数据库类型枚举
    public enum Type{
        MYSQL, ORACLE, SQLSERVER, POSTGRESQL
    }
    // 数据库名称
    private String name;
    // 数据库类型
    private Type type;
    // 数据库概要(数据库中的Schema，为数据库对象的集合，一个用户一般对应一个schema)
    private String schema;
    // 数据库驱动名称
    private String driverClassName;
    // 数据库地址
    private String url;
    // 数据库用户名
    private String userName;
    // 数据库密码
    private String password;
    // 实体类对象集合
    private Map<String, Entity> entityMap = new LinkedHashMap<>();
    // 数据库连接对象
    private Connection connection;

    public Database(
            String name,
            Type type,
            String schema,
            String driverClassName,
            String url,
            String userName,
            String password,
            Connection connection
    ) {
        this.name = name;
        this.type = type;
        this.schema = schema;
        this.driverClassName = driverClassName;
        this.url = url;
        this.userName = userName;
        this.password = password;
        this.connection = connection;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public String getSchema() {
        return schema;
    }

    public void setSchema(String schema) {
        this.schema = schema;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Map<String, Entity> getEntityMap() {
        return entityMap;
    }

    public Set<Entity> getTableSet() {
        return new LinkedHashSet<>(entityMap.values());
    }

    public Entity getTable(String tableName) {
        return entityMap.get(tableName);
    }

    public void setEntityMap(Map<String, Entity> entityMap) {
        this.entityMap = entityMap;
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    @Override
    public String toString() {
        return "Database{" +
                "name='" + name + '\'' +
                ", schema='" + schema + '\'' +
                ", driverClassName='" + driverClassName + '\'' +
                ", url='" + url + '\'' +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", entityMap=" + entityMap +
                '}';
    }
}
